package com.polaris.live.viewmodel

import androidx.lifecycle.MutableLiveData
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.request
import com.polaris.live.common.network.state.ResultState
import com.polaris.live.net.liveService
import com.polaris.live.resp.back_resp.LiveInviteJoinUserResp
import com.polaris.live.resp.base.ListResp
import com.polaris.live.resp.go_resp.InviteJoinRequest
import com.polaris.live.widget.refresh.OnListListener

/**
 * 房间邀请关注
 *
 * @author Created by 天晴 on 2024/11/27 09:15
 * @since 1.0.0
 */
open class RoomInvitationModel : BaseViewModel(), OnListListener {

    val followersLiveData = MutableLiveData<ResultState<ListResp<LiveInviteJoinUserResp>>>()

    val pageSize = 20  // 每页记录数

    private var searchText: String? = null
    private var liveId: Long = 0
    private fun getInviteUser(pageNumber: Int, pageSize: Int = this.pageSize) {
        request({ liveService.getInviteUser(liveId, searchText, pageNumber, pageSize) }, followersLiveData)
    }

    fun searchUser(searchText: String?) {
        this.searchText = searchText
        getInviteUser(pageNumber = 0)
    }

    fun setLiveId(liveId: Long) {
        this.liveId = liveId
    }

    override fun onRefresh() {
        getInviteUser(pageNumber = 0)
    }

    override fun onLoadMore(pageIndex: Int) {
        getInviteUser(pageNumber = pageIndex)
    }

    val inviteJoinLiveData = MutableLiveData<ResultState<Int>>()
    fun inviteJoin(inviteUserId: Long, position: Int) {
        request({ liveService.inviteJoin(InviteJoinRequest(inviteUserId, liveId)) }, {
            inviteJoinLiveData.value = ResultState.onAppSuccess(position)
        }, {
            inviteJoinLiveData.value = ResultState.onAppError(it)
        })
    }

}